package com.shopping.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.common.Result;
import com.shopping.dto.MessageDTO;
import com.shopping.dto.MessageQueryDTO;
import com.shopping.entity.Message;
import com.shopping.service.MessageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/messages")
@PreAuthorize("hasRole('ADMIN')")
public class AdminMessageController {

    @Resource
    private MessageService messageService;

    @GetMapping
    public Result<Page<MessageDTO>> getMessageList(MessageQueryDTO queryDTO) {
        try {
            Page<MessageDTO> page = messageService.getMessageList(queryDTO);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error("获取留言列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Result<MessageDTO> getMessageById(@PathVariable Long id) {
        try {
            MessageDTO message = messageService.getMessageById(id);
            return Result.success(message);
        } catch (Exception e) {
            return Result.error("获取留言详情失败: " + e.getMessage());
        }
    }

    @PostMapping
    public Result<MessageDTO> createMessage(@Valid @RequestBody Message message) {
        try {
            MessageDTO createdMessage = messageService.createMessage(message);
            return Result.success(createdMessage);
        } catch (Exception e) {
            return Result.error("创建留言失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public Result<MessageDTO> updateMessage(
            @PathVariable Long id,
            @Valid @RequestBody Message message
    ) {
        try {
            MessageDTO updatedMessage = messageService.updateMessage(id, message);
            return Result.success(updatedMessage);
        } catch (Exception e) {
            return Result.error("更新留言失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteMessage(@PathVariable Long id) {
        try {
            messageService.deleteMessage(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除留言失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}/status")
    public Result<Void> toggleMessageStatus(@PathVariable Long id) {
        try {
            messageService.toggleMessageStatus(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新留言状态失败: " + e.getMessage());
        }
    }

    @GetMapping("/statistics")
    public Result<Object> getMessageStatistics() {
        try {
            Object statistics = messageService.getMessageStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            return Result.error("获取留言统计信息失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}/reply")
    public Result<Void> replyMessage(@PathVariable Long id, @RequestBody Map<String, String> request) {
        try {
            String reply = request.get("reply");
            if (reply == null || reply.trim().isEmpty()) {
                return Result.error("回复内容不能为空");
            }
            messageService.replyMessage(id, reply);
            return Result.success();
        } catch (Exception e) {
            return Result.error("回复留言失败: " + e.getMessage());
        }
    }
}